package com.yidiandao.business.data.model;

import com.yidiandao.business.constant.Common;
import com.yidiandao.business.constant.Urls;
import com.yidiandao.business.data.net.BaseRequest;
import com.yidiandao.business.data.net.NetBiz;
import com.yidiandao.business.data.net.NetListener;
import com.yidiandao.business.data.net.exception.BizErrorInfo;
import com.yidiandao.business.data.net.exception.NetErrorInfo;
import com.yidiandao.business.data.recyclertype.AccurateContent;
import com.yidiandao.business.data.recyclertype.Article;
import com.yidiandao.business.data.recyclertype.ArticleTitle;
import com.yidiandao.business.data.recyclertype.SearchAccurateContent;
import com.yidiandao.business.data.recyclertype.SearchHistory;
import com.yidiandao.business.data.request.DiscoverySearchAccurateRequest;
import com.yidiandao.business.data.request.DiscoverySearchRequest;
import com.yidiandao.business.data.response.DiscoverySearchAccurateResponse;
import com.yidiandao.business.data.response.DiscoverySearchResponse;
import com.yidiandao.business.data.response.QueryAccurateContentResponse;
import com.yidiandao.business.utils.PreferenceUtils;
import com.yidiandao.business.utils.UIUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * DiscoverySearchModel
 * <p>
 * Created by ChenTao(chentao7v@gmail.com) on 2017/4/13 0013 14:31
 */
public class DiscoverySearchModel implements IDiscoverySearchModel {

    private NetBiz mNetBiz = new NetBiz();

    private List<Object> mSearchResult = new ArrayList<>();

    private List<Article> mSearchAccurateResult = new ArrayList<>();

    private int searchAccuratePager;

    @Override
    public SearchHistory loadSearchHistory() {
        List<String> history = PreferenceUtils.getDiscoverySearchHistory(UIUtils.getContext());
        if (history.size() != 0) {
            SearchHistory searchHistory = new SearchHistory();
            searchHistory.setHistory(history);
            return searchHistory;
        } else {
            return null;
        }
    }

    @Override
    public void loadAccurateContent(String pagerTag, final OnLoadResultListener<SearchAccurateContent> listener) {
        BaseRequest request = new BaseRequest();
        request.setUrl(Urls.DISCOVERY_CAT_LIST);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        request.setTag(pagerTag);
        mNetBiz.sendGetRequest(request, QueryAccurateContentResponse.class, new NetListener<QueryAccurateContentResponse>() {
            @Override
            public void onSuccess(QueryAccurateContentResponse response) {
                List<AccurateContent> accurateContents = response.getData().getAccurateContents();
                if (listener != null) {
                    SearchAccurateContent searchAccurateContent = new SearchAccurateContent();
                    searchAccurateContent.setContent(accurateContents);
                    listener.onSuccess(searchAccurateContent, false, false);
                }
            }

            @Override
            public boolean onError(NetErrorInfo netErrorInfo) {
                if (listener != null) {
                    listener.onFailed();
                }
                return false;
            }

            @Override
            public boolean onFailed(BizErrorInfo bizErrorInfo) {
                if (listener != null) {
                    listener.onFailed();
                }
                return false;
            }

            @Override
            public void onFinal() {

            }
        });
    }

    @Override
    public void saveSearchHistory(String keyword) {
        PreferenceUtils.saveDiscoverySearchHistory(UIUtils.getContext(), keyword);
    }

    @Override
    public void commonSearch(String tag, final String keyword, final OnLoadResultListener<List<Object>> listener) {
        DiscoverySearchRequest request = new DiscoverySearchRequest();
        request.setTag(tag);
        request.setUrl(Urls.DISCOVERY_SEARCH);
        request.setKeyword(keyword);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));

        mNetBiz.sendGetRequest(request, DiscoverySearchResponse.class, new NetListener<DiscoverySearchResponse>() {
            @Override
            public void onSuccess(DiscoverySearchResponse response) {

                // 保留搜索历史
                saveSearchHistory(keyword);

                mSearchResult.clear();
                List<DiscoverySearchResponse.Data> data = response.getData();
                if (data != null && data.size() != 0) {
                    for (int i = 0; i < data.size(); i++) {
                        DiscoverySearchResponse.Data categoryArticle = data.get(i);
                        List<Article> articleList = categoryArticle.getArticleList();
                        if (articleList == null || articleList.size() == 0) {
                            continue;
                        }

                        mSearchResult.add(new ArticleTitle(categoryArticle.getCatName(), categoryArticle.getCatId(), keyword));
                        mSearchResult.addAll(articleList);
                    }
                }
                if (listener != null) {
                    listener.onSuccess(mSearchResult, false, false);
                }
            }

            @Override
            public boolean onError(NetErrorInfo netErrorInfo) {
                if (listener != null) {
                    listener.onFailed();
                }
                return true;
            }

            @Override
            public boolean onFailed(BizErrorInfo bizErrorInfo) {
                if (listener != null) {
                    listener.onFailed();
                }
                return true;
            }

            @Override
            public void onFinal() {

            }
        });
    }

    @Override
    public void searchAccurate(String tag, final boolean isRefreshing, String extraId, final String keyword, final OnLoadResultListener<List<Article>> listener) {

        if (isRefreshing) {
            searchAccuratePager = 1;
        }

        DiscoverySearchAccurateRequest request = new DiscoverySearchAccurateRequest();
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        request.setTag(tag);
        request.setKeyword(keyword);
        request.setExtraId(extraId);
        request.setUrl(Urls.SEARCH_ACCURATE);
        request.setPage(searchAccuratePager);
        request.setPageSize(Common.PAGER_SIZE);

        mNetBiz.sendGetRequest(request, DiscoverySearchAccurateResponse.class, new NetListener<DiscoverySearchAccurateResponse>() {
            @Override
            public void onSuccess(DiscoverySearchAccurateResponse response) {

                // 保留搜索历史
                saveSearchHistory(keyword);

                searchAccuratePager++;
                if (isRefreshing) {
                    mSearchAccurateResult.clear();
                }

                List<Article> articleList = response.getData().getArticleList();
                mSearchAccurateResult.addAll(articleList);

                if (listener != null) {
                    listener.onSuccess(mSearchAccurateResult, articleList.size() == 0, response.hasMoreData());
                }
            }

            @Override
            public boolean onError(NetErrorInfo netErrorInfo) {
                if (listener != null) {
                    listener.onFailed();
                }
                return true;
            }

            @Override
            public boolean onFailed(BizErrorInfo bizErrorInfo) {
                if (listener != null) {
                    listener.onFailed();
                }
                return true;
            }

            @Override
            public void onFinal() {

            }
        });
    }

    @Override
    public void clearHistory() {
        PreferenceUtils.clearDiscoverySearchHistory(UIUtils.getContext());
    }
}
